<% layout('../../includes/layout-form-v2') -%>

<div class="box-body">


	<form id="form" class="order-reference"
		action='/frontend/<%-controller%>/<%- action %>/<%- id %>'
		method='post'>
		<div class="input-group input-group">
			<p class="error_msg"></p>
			
		
				
			<span class="input-group-addon"><b>Order Reference </b></span> <input
				type="text" id="orderid" name="orderid" class="form-control">
			<span class="input-group-btn"> <input type="submit"
				class="btn btn-primary btn " value="Search">
			</span>
			<input type="hidden" id="userid" >
		</div>
	</form>

	<br />
	<form id="form" class="receive-payment"
		action='/frontend/<%-controller%>/<%- action %>/<%- id %>'
		method='post'>
	
		<div class="alert alert-danger alert-dismissable" id="order-reference-error">
            <i class="fa fa-ban"></i>
            
            <b>Error!</b> Order Reference not found.
        </div>
	

		<div id="order-payment-details">
			<div class="box box-info">
				<div class="box-header">
					<h3 class="box-title">User Information</h3>
				</div>

				<div class="row">
					<div class="col-xs-3">
						<label>Username :</label>
					</div>
					<p class="pull-left" id="username"></p>
				</div>


				<div class="row">
					<div class="col-xs-3">
						<label>First Name :</label>
					</div>
					<p class="pull-left" id="firstname"></p>
				</div>


				<div class="row">
					<div class="col-xs-3">
						<label>Last Name :</label>
					</div>
					<p class="pull-left" id="lastname"></p>
				</div>

				<div class="row">
					<div class="col-xs-3">
						<label>Email :</label>
					</div>
					<p class="pull-left text-light-blue" id="email"></p>
				</div>

			</div>

			<div class="box box-info">
				<div class="box-header">
					<h3 class="box-title">Order Details</h3>
				</div>

				<div class="row">
					<div class="col-xs-3">
						<label>Description :</label>
					</div>
					<p class="pull-left" id="itemdescription"></p>
				</div>


				<div class="row">
					<div class="col-xs-3">
						<label>Item Name :</label>
					</div>
					<p class="pull-left " id="itemname"></p>
				</div>
			</div>

			<div class="box box-info">
				<div class="box-header">
					<h3 class="box-title">Note</h3>
				</div>


				<div class="form-group">
					<textarea id="note" class="form-control" rows="4"
						placeholder="Enter note"></textarea>
				</div>

			</div>

			<div class="box-footer">
				<input type="submit" class="btn btn-primary btn-lg btn-receive-payment" value="Receive Payment">
			</div>
		</div>
	</form>
</div>
<!-- /.box-body -->

<script>
$(document).on("submit", ".order-reference", function(e) {
	e.preventDefault();
	// Bid Pack
	/*var parameters = {
		search_for : 'bidpack'
	};*/
	

	// Payment
	var parameters = {
		search_for : 'payment_order',
		orderid : $('#orderid').val()
	};

	// Users
	/*var parameters = {
		search_for : 'registration',
		firstname : $('#firstname').val(),
		lastname : $('#lastname').val(),
	};*/

	$.ajax({
		url : "<%= refData.settingsList[0]['url'] %>",
		type : 'get',
		data : parameters,
		dataType : 'json',

		success : function(json) {

			console.log(json);

			if (json == "") {
				$("#order-payment-details").hide();
				$("#order-reference-error").fadeIn(300);

			}

			else {

				$("#order-reference-error").hide();
				$("#username").html(json[0].username);

				$("#firstname").html(json[0].firstname);
				$("#lastname").html(json[0].lastname);
				$("#email").html(json[0].email);

				$("#itemdescription").html(json[0].itemdescription);
				$("#itemname").html(json[0].itemname);

				$("#userid").val(json[0].userid);

				$("#order-payment-details").fadeIn(500);
				
				$(".btn-receive-payment").fadeIn();
				$(".btn-receive-payment").attr("disabled" , false);
				
			}

		},

		error : function(json) {
			console.log("Something's wrong!");
		}

	});

});





//taga bato ng json
$(document).on("submit", ".receive-payment", function(e) {
	e.preventDefault();
	
	
	
	if($("#note").val() == "") {
		alert("Note field is required.");
		return false;
	}

	
	$(".btn-receive-payment").attr("disabled" , true);
	
	var parameters = {
		submit_to : 'payment_order',
		orderid : $('#orderid').val(),
		userid : $('#userid').val(),
		note : $('#note').val(),
		company_user_id : <%- currentUser %>,
		branch_id : <%- currentBranchId %>,
		company_key : "<%- currentCompanyKey %>"
	};

	$.ajax({
		type : "post", 
		url : "<%= refData.settingsList[0]['url'] %>",
		data : parameters,
		dataType : 'json',
		success : function(msg) {
			
			
			
			if(msg.success) {
				alert(msg.success);
				$(".btn-receive-payment").fadeOut();
			} else {
				alert("Error");
				$(".btn-receive-payment").attr("disabled" , false);
			}
			

			console.log(msg);
		},
		error : function(msg) {
			alert("Error in database");
			console.log("Something's wrong!");
		}
	});

});
</script>
